package com.petshop.controller;

import com.petshop.dto.CartItemRequest;
import com.petshop.dto.CartItemUpdateDTO;
import com.petshop.pojo.CartItems;
import com.petshop.service.CartItemsService;
import com.petshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("cartItem")
@CrossOrigin
public class CartItemsController {
    @Autowired
    private CartItemsService cartItemsService;

    /*
    添加一个商品到购物车
     */
    @PostMapping("addItem")
    public Result addItem(@RequestBody CartItemRequest cartItemRequest, @RequestHeader("Authorization") String token) {
        Result result = cartItemsService.addItem(cartItemRequest, token);
        return result;
    }

    /*
    从购物车批量删除商品
     */
    @PostMapping("outItem")
    public Result outItem(@RequestBody List<Integer> cartIds) {
        Result result = cartItemsService.outItem(cartIds);
        return result;
    }

    /*
    根据id从购物车删除1个商品
     */
    @GetMapping("outOneItem")
    public Result outOneItem(Integer cartId) {
        Result result = cartItemsService.outOneItem(cartId);
        return result;
    }

    /*
    获取购物车的所有商品
     */
    @GetMapping("getAllItem")
    public Result getAllItem(@RequestHeader("Authorization") String token) {
        Result result = cartItemsService.getAllItem(token);
        return result;
    }

    /*
    修改购物车商品数量
     */
    @PostMapping("updateQuantity")
    public Result updateQuantity(@RequestBody CartItemUpdateDTO cartItemUpdateDTO) {
        Result result = cartItemsService.updateQuantity(cartItemUpdateDTO);
        return result;
    }

    /*
    根据主键id数组批量查询购物车商品
     */
    public List<CartItems> getCartItems(ArrayList<Integer> cartIds) {
        return cartItemsService.selectCartItems(cartIds);
    }
}
